<template>
  <div class="my-container">
    <div v-if="user" class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            :src="currentUser.photo"
            round
            fit="cover"
          />
          <span class="name">{{ currentUser.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/userProfile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{ currentUser.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{ currentUser.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{ currentUser.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{ currentUser.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push({name:'login',query:{redirect:'/my'}})">
        <img class="mobile-img" src="../assets/images/mobile.png" alt="" />
        <span class="text">登录/注册</span>
      </div>
    </div>

    <van-grid class="grid-item mb-4" :column-num="2" clickable>
      <van-grid-item
        class="nav-grid-item"
        icon="star-o"
        text="收藏"
        style="color:#eb5253"
      />
      <van-grid-item
        class="nav-grid-item"
        icon="underway-o"
        text="历史"
        style="color:#ff9d1d"
      />
    </van-grid>

    <van-cell title="消息通知" is-link to="/" />
    <van-cell class="mb-4" title="小智同学" is-link to="/userchat" />

    <van-cell
      v-if="user"
      class="logout-cell"
      title="退出登录"
      @click="onLogout"
    />
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getCurrentUser } from "../api/user.js";
export default {
  name: "my",
  data() {
    return {
      currentUser: {} //当前用户登录信息
    };
  },
  computed: {
    ...mapState(["user"])
  },
  created() {
    this.loadCurrentUser();
  },
  mounted() {},
  methods: {
    onLogout() {
      this.$dialog
        .confirm({
          title: "退出提示",
          message: "确认退出吗?"
        })
        .then(() => {
          //确认
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          //取消
          // on cancel
        });
    },
    //获取用户登录信息
    async loadCurrentUser() {
      const { data: res } = await getCurrentUser();
      this.currentUser = res.data;
    }
  }
};
</script>
<style scoped lang="less">
.my-container {
  .header {
    height: 180px;
    background: url("../assets/images/banner.png");
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      .mobile-img {
        width: 66px;
        height: 66px;
        margin-bottom: 8px;
      }
      .text {
        font-size: 14px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 115px;
      padding: 38px 16px 11px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 66px;
          height: 66px;
          margin-right: 12px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 15px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        flex: 1;
        height: 65px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
      }
      .count {
        font-size: 18px;
      }
      .text {
        font-size: 12px;
      }
    }
  }
  .grid-item {
    .nav-grid-item {
      height: 70px;
    }
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
  }
  .mb-4 {
    margin-bottom: 4px;
  }
}
</style>
